package tq.com.tqteacher.activity.newPublish;

import android.graphics.Color;
import android.os.Bundle;
import android.print.PrinterId;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnOptionsSelectChangeListener;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.githang.statusbar.StatusBarCompat;

import java.util.ArrayList;

import tq.com.systools.system.AlertDialogHelper;
import tq.com.systools.system.StringHelper;
import tq.com.tqcom.base.BaseActivity;
import tq.com.tqcom.base.BaseFragment;
import tq.com.tqcom.base.BaseTitleActivity;
import tq.com.tqcom.net.INetCallback;
import tq.com.tqcom.net.TQNetResult;
import tq.com.tqcom.util.TQEventBus;
import tq.com.tqcom.util.TQSystemDefine;
import tq.com.tqcom.view.TQHeader5View;
import tq.com.tqteacher.R;
import tq.com.tqteacher.model.TQLogin;
import tq.com.tqteacher.model.TQPublishType;
import tq.com.tqteacher.model.TQWKPublish;
import tq.com.tqteacher.view.TQBasePublishInfoView;
import tq.com.tqteacher.view.TQFanweiView;
import tq.com.tqteacher.view.TQHeader3ViewHold;

public class TQWKPublishActivity extends BaseTitleActivity {
    private TQWkPublishFragment mWkPublishFragment = new TQWkPublishFragment();

    @Override
    protected BaseFragment fragment() {
        return mWkPublishFragment;
    }

    @Override
    protected void initHeader(TQHeader5View pHeader5View) {
        super.initHeader(pHeader5View);
        pHeader5View.getTitleView().setText("新建微课");
    }

    static public class TQWkPublishFragment extends BaseFragment{
        private EditText mTitle;
        private EditText mGrade;
        private TQFanweiView mFanweiView;
        private OptionsPickerView mOptionsPickerView;
        private ArrayList<TQBasePublishInfoView.GradData> mGradDataArrayList = new ArrayList<>();

        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View lView = inflater.inflate(R.layout.activity_wk_publish_layout, container, false);
            lView.findViewById(R.id.next).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    next();
                }
            });

            return lView;
        }

        @Override
        public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);

            mTitle = view.findViewById(R.id.title_input);
            mGrade = view.findViewById(R.id.grad_input);
            mFanweiView = view.findViewById(R.id.area_panel);

            mGrade.setFocusable(false);
            mGrade.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });

            initGrad();
            if(TQPublishType.getSharePublishItem().isModify()
                    && TQPublishType.getSharePublishItem() instanceof TQWKPublish){
                bindData();
            }
        }

        private void initGrad(){
            int grade = TQLogin.getCurrent().getUserInfo().getGrade();
            String gradeName = TQSystemDefine.gradeName(grade);

            mGrade.setText(gradeName);
        }

        private void next(){
            String ltitle = StringHelper.editText(mTitle);
            if(StringHelper.isEmpty(ltitle)){
                AlertDialogHelper.show(this.getContext(), "标题不能为空");
                return;
            }
            int fanwei = mFanweiView.getSelect();

            final TQWKPublish lTQWKPublish = (TQWKPublish)TQPublishType.getSharePublishItem();
            lTQWKPublish.setName(ltitle);
            lTQWKPublish.setGrade(TQLogin.getCurrent().getUserInfo().getGrade());
            lTQWKPublish.setShow(fanwei);

            if(lTQWKPublish.isModify()){
                lTQWKPublish.onModifyBaseInfo(new INetCallback() {
                    @Override
                    public void result(TQNetResult pNetResult) {
                        if(!pNetResult.isSuccess()){
                            AlertDialogHelper.show(TQWkPublishFragment.this.getContext(), pNetResult.getErrMsg());
                            return;
                        }

                        TQEventBus.post(new TQEventBus.TQEventMessage(TQEventBus.TQEventMessage.Opertor.modify, null));
                        TQWkPublishFragment.this.getActivity().finish();
                    }
                });
            }else{
                lTQWKPublish.onPulishComplete(new INetCallback() {
                    @Override
                    public void result(TQNetResult pNetResult) {
                        Log.d(TAG, "result: ");
                        if(!pNetResult.isSuccess()){
                            AlertDialogHelper.show(TQWkPublishFragment.this.getContext(), pNetResult.getErrMsg());
                            return;
                        }

                        lTQWKPublish.onNewPublishResult(pNetResult.getData());
                        TQEventBus.post(new TQEventBus.TQEventMessage(TQEventBus.TQEventMessage.Opertor.add, null));
                        TQPublishType.showPublishNextStep(TQWkPublishFragment.this.getActivity());

                        TQWkPublishFragment.this.getActivity().finish();
                    }
                });
            }
        }

        private void bindData(){
            mTitle.setText(TQPublishType.getSharePublishItem().getName());

            int grade = TQPublishType.getSharePublishItem().getGrade();
            String gradeName = TQSystemDefine.gradeName(grade);
            mGrade.setText(gradeName);


            int fanwei = TQPublishType.getSharePublishItem().getShow();
            mFanweiView.setSelect(fanwei);
        }
    }
}
